const express = require('express');
const app = express();

/** 什么都不限制 **/

app.use(function (req, res, next) {
  console.log('Time:', Date.now())
  next()
})
/** **/

/** 限制请求路径 **/
// app.use('/user/:id', function (req, res, next) {
//   console.log('Request Type:', req.method)
//   next()
// })
/** **/

/** 限制请求路径+请求方法 **/
// app.get('/user/:id', function (req, res, next) {
//   res.send('USER')
// })
/** **/

/** 同一个路径自定义多个中间件 **/
/** **/
// app.use('/user/:id', function (req, res, next) {
//   console.log('Request URL:', req.originalUrl)
//   next()
// }, function (req, res, next) {
//   console.log('Request Type:', req.method)
//   next()
// })
/** **/

/** id为0的请求 **/
app.get('/user/:id', function (req, res, next) {
  // if the user ID is 0, skip to the next route
  if (req.params.id === '0') next('route')
  // otherwise pass the control to the next middleware function in this stack
  else next()
}, function (req, res, next) {
  // send a regular response
  res.send('regular')
})
/** **/

// // handler for the /user/:id path, which sends a special response
app.get('/user/:id', function (req, res, next) {
  res.send('special')
})
/** **/


/** 中间件数组 **/
// function logOriginalUrl(req, res, next) {
//   console.log('Request URL:', req.originalUrl)
//   next()
// }

// function logMethod(req, res, next) {
//   console.log('Request Type:', req.method)
//   next()
// }

// const logStuff = [logOriginalUrl, logMethod]
// app.get('/user/:id', logStuff, function (req, res, next) {
//   res.send('User Info')
// })
/** **/

app.listen(9000, () => {
  console.log('middware app start');
})